package com.jiepos.mpos.biz.appInfM.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.ClientHelpDao;
import com.jiepos.mpos.common.entity.App_Client_HelpEntity;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.common.entity.GpsEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;


/**
 *
 *
 * <p>标题: —— 帮助信息维护维护类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.appInfM.service
 * @author 余翼膑
 * @version 1.0
 * @since 2017年9月22号
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class HelpService extends BaseService {
    @Autowired
    private ClientHelpDao clientHelpDao;

    /**
     * 查询 帮助信息维护列表json分页数据
     *
     * @param pagination
     * @param helpEntity
     * @return
     */
    public Map getHelpServiceList(HttpServletRequest request, Pagination pagination, App_Client_HelpEntity helpEntity, App_Ins_InfEntity appInsInfEntity) {
        StringBuffer sb = getInitSql("t1").append(",")
                .append("  T1.HELP_UUID AS UUID,  ")
                .append("  t2.ins_cd||'-'||t2.ins_cn_nm  as inst_name, ")
                .append("  T3.APP_NAME_APP, ")
                .append("  T1.TITLE, ")
                .append("  T1.HELP_TP||'-'|| T4.CODE_DESC as HELP_TP ")
                .append("  from T_APP_CLIENT_HELP T1 LEFT JOIN T_APP_INS_INF T2 ON T1.INS_UUID = T2.INS_UUID ")
                .append("  LEFT JOIN T_APP_INS_APP T3 ON T1.INS_UUID = T3.INS_UUID ")
                .append(" LEFT JOIN T_APP_CODE_LIST T4 ON  T1.help_tp = t4.code_val and t4.code_tp='HELP_TP' ")
                .append(" where 1=1 ");
        List list = new ArrayList();
        //标题
        if (!StringUtils.isBlank(helpEntity.getTitle())) {
            sb.append(" and t1.title like ? ");
            list.add("%" + helpEntity.getTitle() + "%");
        }
        //机构号
        if (!StringUtils.isBlank(appInsInfEntity.getIns_cd())) {
            sb.append(" and t2.ins_cd like ? ");
            list.add("%" + appInsInfEntity.getIns_cd() + "%");
        }
        //机构名称
        if (!StringUtils.isBlank(appInsInfEntity.getIns_cn_nm())) {
            sb.append(" and t2.ins_cn_nm like ? ");
            list.add("%" + appInsInfEntity.getIns_cn_nm() + "%");
        }
        pagination.queryString = sb.toString();
        pagination = getInsAuthSql(request, pagination, "t1", null);
        pagination = this.getOrderSql(pagination, "t1");
        return clientHelpDao.getPaginationMap(pagination, "t1", list);
    }

    /**
     * 根据记录id查询实体对象
     *
     * @param help_uuid
     * @return
     */
    public Map getHelpById(String help_uuid) {
        if (StringUtils.isBlank(help_uuid) || help_uuid.equals("undefined")) {
            return new HashMap();
        }
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append(" select  ")
                .append("  T1.help_uuid, ")
                .append("  T1.ins_uuid, ")
                .append("  t2.ins_cd||'-'||t2.ins_cn_nm  as inst_name, ")
                .append("  T1.TITLE, ")
                .append("  t1.help_tp, ")
                .append("  T1.CONTENT, ")
                .append("  T1.REC_ID ")
                .append("  from T_APP_CLIENT_HELP T1 LEFT JOIN T_APP_INS_INF T2 ON T1.INS_UUID = T2.INS_UUID ")
                .append(" WHERE T1.help_uuid = ?");
        List<String> list = new ArrayList<>();
        list.add(help_uuid);
        List<Map<String, Object>> results = clientHelpDao.queryForList(getOrderSql(sqlBuf.toString(), "t1"), list);
        return results != null ? results.get(0) : new HashMap<String, Object>();
    }

    /**
     * 保存客户端维护
     *
     * @param helpEntity
     * @param map
     */
    public void saveHelp(App_Client_HelpEntity helpEntity, Map map) {
        //当前用户
        helpEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
        if (!StringUtils.isBlank(helpEntity.getHelp_uuid())) {
            //新增
            helpEntity = (App_Client_HelpEntity) this.saveCommonData(helpEntity, map, "update");
            List noChangeList = new ArrayList();
            noChangeList.add("help_uuid");
            noChangeList.add("rec_crt_ts");
            clientHelpDao.update(helpEntity, noChangeList);
        } else {
            helpEntity.setLast_oper_in(SystemConstant.OPER_ADD);
            helpEntity.setHelp_uuid(IDFactoryUtil.getUUID());
            App_Client_HelpEntity newHelpEntity = (App_Client_HelpEntity) this.saveCommonData(helpEntity, map, "null");
            clientHelpDao.save(helpEntity);
        }
    }

    /**
     * 客户端帮助信息列表
     *
     * @param gpsEntity
     * @return
     */
    public List<Map<String, Object>> getHelpList(String loginId) {
    	StringBuffer sql = new StringBuffer();
    	sql.append("SELECT HELP_UUID AS \"help_uuid\", TITLE AS \"title\", HELP_TP AS \"help_tp\"")
    	.append("  FROM ").append(Table.T_APP_CLIENT_HELP)
    	.append(" WHERE INS_UUID =")
    	.append("       (SELECT INS_UUID")
    	.append("          FROM ").append(Table.T_APP_INS_INF)
    	.append("         WHERE ").append(SystemConstant.REC_ST).append(" = ?")
    	.append("           AND INS_ST = '1'")
    	.append("           AND ROWNUM = 1")
    	.append("           AND INS_UUID IN")
    	.append("               (SELECT INS_UUID FROM ").append(Table.T_APP_CLIENT_HELP).append(" WHERE ").append(SystemConstant.REC_ST).append(" = ?)")
    	.append("         START WITH INS_UUID = (SELECT INS_UUID")
    	.append("                                  FROM ").append(Table.T_APP_CUSTOM_INF)
    	.append("                                 WHERE LOGIN_ID = ?)")
    	.append("        CONNECT BY PRIOR SUP_INS_UUID = INS_UUID)");
        return clientHelpDao.queryForListMap(getOrderSql(sql.toString(), null), new Object[]{SystemConstant.REC_ST_1, SystemConstant.REC_ST_1, loginId});
    }

    /**
     * 客户端帮助详细信息
     *
     * @param gpsEntity
     * @param help_uuid
     * @return
     */
    public Map getHelpDetail(GpsEntity gpsEntity, String help_uuid) {
            StringBuffer sqlBuf = new StringBuffer();
            sqlBuf.append(" select  ")
                    .append("  T1.help_uuid as \"help_uuid\", ")
                    .append("  T1.TITLE as \"title\", ")
                    .append("  t1.help_tp as \"help_tp\", ")
                    .append("  t1.content as \"content\" ")
                    .append("  from " + DBConstant.Table.T_APP_CLIENT_HELP + " T1 where t1.help_uuid = ? ");
            List insParamList = new ArrayList();
            insParamList.add(help_uuid);
        List<Map<String, Object>> results  = clientHelpDao.queryForList(getOrderSql(sqlBuf.toString(), "t1"), insParamList);
        return results != null ? results.get(0) : new HashMap<String, Object>();
    }

    /**
     * 获取某个类型的客户端帮助信息详细信息
     *
     * @param ins_uuid
     * @param help_tp
     * @return
     */
    public Map<String, Object> getHelpDetailByTp(String ins_uuid, String help_tp) {
        StringBuffer sql = new StringBuffer();
    	sql.append("SELECT HELP_UUID AS \"help_uuid\", TITLE AS \"title\", HELP_TP AS \"help_tp\", CONTENT AS \"content\"")
    	.append("  FROM ").append(Table.T_APP_CLIENT_HELP)
    	.append(" WHERE INS_UUID =")
    	.append("       (SELECT INS_UUID")
    	.append("          FROM ").append(Table.T_APP_INS_INF)
    	.append("         WHERE ").append(SystemConstant.REC_ST).append(" = ?")
    	.append("           AND INS_ST = '1'")
    	.append("           AND ROWNUM = 1")
    	.append("           AND INS_UUID IN")
    	.append("               (SELECT INS_UUID FROM ").append(Table.T_APP_CLIENT_HELP).append(" WHERE ").append(SystemConstant.REC_ST).append(" = ?)")
    	.append("         START WITH INS_UUID = ?")
    	.append("        CONNECT BY PRIOR SUP_INS_UUID = INS_UUID)")
    	.append("   AND HELP_TP = ?");
    	List<Map<String, Object>> results = clientHelpDao.queryForListMap(sql.toString(), new Object[]{SystemConstant.REC_ST_1, SystemConstant.REC_ST_1, ins_uuid, help_tp});
    	return results != null ? results.get(0) : new HashMap<String, Object>();
    }

    /**
     * 删除方法
     * @param help_uuids 主键唯一标识
     */
    public void deleteHelpByIds(String help_uuids) {
        clientHelpDao.deleteByStatus("help_uuid",help_uuids);
    }
}
